import { DbService } from './db.service';
import { XlsxService } from './../../services/xlsx.service';
import { UnzipService } from './../../services/unzip.service';
import { DeleteService } from './../../services/delete.service';
import { Injectable } from '@nestjs/common';

import * as fs from 'fs';
@Injectable()
export class FractionService {
  constructor(
    private readonly deleteService:DeleteService,
    private readonly unzipService:UnzipService,
    private readonly xlsxService:XlsxService,
    private readonly dbService:DbService,
  ) {}

  async upload(file, dept){
    this.deleteService.deleteall('src/excels'); // 删除excels文件
    const zip = new Buffer(file.buffer, 'base64');
    const writerStream = fs.createWriteStream('src/public/excels.zip');
    writerStream.write(zip);
    writerStream.end();

    const data = await new Promise(resolve => {
        writerStream.on('finish', () => {
            console.log('写入完成。');
            this.unzipService.Unzip('src/public/excels.zip'); // 解压
            resolve(this.xlsxService.GotObjArr('src/excels', dept));
        });
    });
    return data;
  }

  async find(params){
    return await this.dbService.find(params);
  }
}